import unittest
import logging
from random import random

import requests

from api.loginAPI import loginAPI
from api.trustAPI import trustAPI
from utils import assert_utils, request_third_api



class trust(unittest.TestCase):
    phone = '17688880023'

    def setUp(self) -> None:
        self.login_api = loginAPI()
        self.trust_api = trustAPI()
        self.session = requests.Session()

    def tearDown(self) -> None:
        self.session.close()

    # 开户请求
    def test01_trust_request(self):
        response = self.login_api.login(self.session,self.phone)
        logging.info("login response = {}".format(response.json()))
        assert_utils(self,response,200,200,"登录成功")

        response = self.trust_api.trust_register(self.session)
        logging.info("trust_register response = {}".format(response.json()))
        self.assertEqual(200,response.status_code)
        self.assertEqual(200,response.json().get("status"))

        form_data = response.json().get("description").get("form")
        logging.info("form response = {}".format(form_data))
        response = request_third_api(form_data)

        self.assertEqual(200,response.status_code)
        self.assertEqual('UserRegister OK',response.text)

    # 充值成功
    def test02_recharge_success(self):
        response = self.login_api.login(self.session, self.phone)
        logging.info("login response = {}".format(response.json()))
        assert_utils(self, response, 200, 200, "登录成功")

        # 获取充值验证码
        r = random()
        response = self.trust_api.get_recharge_verify_code(self.session,str(r))
        self.assertEqual(200,response.status_code)

        # 发送充值请求
        response = self.trust_api.recharge(self.session,'100000')
        logging.info("recharge response = {}".format(response.json()))
        self.assertEqual(200,response.status_code)
        self.assertEqual(200,response.json().get("status"))

        # 发送第三方充值请求
        form_data = response.json().get("description").get("form")
        logging.info('form response = {}'.format(form_data))

        response = request_third_api(form_data)
        self.assertEqual('NetSave OK',response.text)










